package cn.fzu;

import cn.fzu.frame.StartFrame;
import cn.fzu.label.MineLabel;
import cn.fzu.panel.MineField;
import cn.fzu.panel.MineState;
import cn.fzu.util.Tools;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * ClassName : cn.fzu.MineLabelListener
 * Desc : TODO
 *
 * @author : LINPQ
 * @date : 2021/3/28 14:21
 */
public class MineLabelListener extends MouseAdapter {

    private StartFrame startFrame;

    public MineLabelListener(StartFrame startFrame) {
        this.startFrame = startFrame;
    }

    boolean first = true;

    @Override
    public void mousePressed(MouseEvent e) {

        MineLabel mineLabel = (MineLabel) e.getSource();

        if (InputEvent.BUTTON1_DOWN_MASK + InputEvent.BUTTON3_DOWN_MASK == e.getModifiersEx()) {
        } else if (InputEvent.BUTTON1_MASK == e.getModifiers()) {
            if (first) {
                buildMine(mineLabel.getRow(), mineLabel.getCol());
            }
            if(!mineLabel.isExpand()){
                mineLabel.setIcon(Tools.iiAroundCount[0]);
            }

        } else if (InputEvent.BUTTON3_MASK == e.getModifiers()) {
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        MineLabel mineLabel = (MineLabel) e.getSource();

        if (InputEvent.BUTTON1_DOWN_MASK + InputEvent.BUTTON3_DOWN_MASK == e.getModifiersEx()) {
        } else if (InputEvent.BUTTON1_MASK == e.getModifiers()) {
            if (first) {
                first = false;
                startFrame.getTimer().start();
            }


            if (mineLabel.isMine()){
                mineLabel.setIcon(Tools.iiblood);
                fail();
                mineLabel.setIcon(Tools.iiblood);
//                JDialog jDialog = new JDialog();
//                jDialog.setTitle("shibaile");
//                jDialog.setLocation(300,300);
//                jDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
//                jDialog.setVisible(true);
//                JOptionPane.showMessageDialog(null, Tools.diuren);
            }else if(mineLabel.isExpand()){


            }else {
                openMine(mineLabel);
            }

            win();

        } else if (InputEvent.BUTTON3_MASK == e.getModifiers()) {
        }

    }

    private void openMine(MineLabel mineLabel) {
        if(!mineLabel.isExpand()){
            mineLabel.setIcon(Tools.iiAroundCount[mineLabel.getAroundCounts()]);
            mineLabel.setExpand(true);
            if(mineLabel.getAroundCounts()==0){
                openAround(mineLabel);
            }
        }

    }

    private void openAround(MineLabel mineLabel) {
        int x =mineLabel.getRow();
        int y = mineLabel.getCol();
        for (int i = Math.max(x - 1, 0); i <= Math.min(x + 1, Tools.rows - 1); i++) {
            for (int j = Math.max(y - 1, 0); j <= Math.min(y + 1, Tools.cols - 1); j++) {
                openMine(startFrame.getMineField().getMineLabels()[i][j]);
            }
        }
    }


    public void buildMine(int row, int col) {
        for (int i = 0; i < Tools.totalMine; ) {
            int x = (int) (Math.random() * Tools.rows);
            int y = (int) (Math.random() * Tools.cols);
            if (x == row && y == col) {

            } else if (isMine(x, y)) {

            } else {
                setMine(x, y);
                i++;
            }
        }
        setArountCount();

//        openAllMine();

    }



    public boolean isMine(int x, int y) {
        if (startFrame.getMineField().getMineLabels()[x][y].isMine()) {
            return true;
        } else {
            return false;
        }
    }


    public void setMine(int x, int y) {
        startFrame.getMineField().getMineLabels()[x][y].setMine(true);
    }

    public void openAllMine() {
        for (int i = 0; i < Tools.rows; i++) {
            for (int j = 0; j < Tools.cols; j++) {
                if (startFrame.getMineField().getMineLabels()[i][j].getAroundCounts() != 0) {
                    startFrame.getMineField().getMineLabels()[i][j].setIcon(Tools.iiAroundCount[startFrame.getMineField().getMineLabels()[i][j].getAroundCounts()]);
                }

                if (isMine(i, j)) {
                    startFrame.getMineField().getMineLabels()[i][j].setIcon(Tools.iiMine);
                }
            }
        }
    }

    public void setArountCount() {
        for (int i = 0; i < Tools.rows; i++) {
            for (int j = 0; j < Tools.cols; j++) {
                startFrame.getMineField().getMineLabels()[i][j].setAroundCounts(countMine(i, j));
            }
        }
    }

    public int countMine(int x, int y) {
        int count = 0;
        for (int i = Math.max(x - 1, 0); i <= Math.min(x + 1, Tools.rows - 1); i++) {
            for (int j = Math.max(y - 1, 0); j <= Math.min(y + 1, Tools.cols - 1); j++) {
                if (isMine(i, j)) {
                    count++;
                }
            }
        }
        return count;
    }

    /**
     * 1. 提示失败
     * 2. 展开全部雷
     * 3. 点到的雷变红底
     * 4. 笑脸变化
     * 5. 时间暂停
     * 6. 雷区不能点击
     */
    public void fail(){
        JOptionPane.showMessageDialog(null, Tools.diuren);
        openAllMine();
        startFrame.getMineState().getFace().setIcon(Tools.iiFace[3]);
        startFrame.getTimer().stop();
        for (int i = 0; i < Tools.rows ; i++) {
            for (int j = 0; j < Tools.cols; j++) {
                startFrame.getMineField().getMineLabels()[i][j].removeMouseListener(startFrame.getMineField().getMineLabelListener());
            }
        }
    }

    public void win(){
        int count = 0;
        for (int i = 0; i < Tools.rows ; i++) {
            for (int j = 0; j < Tools.cols; j++) {
                if(startFrame.getMineField().getMineLabels()[i][j].isExpand()==false){
                    count++;
                }
            }
        }


        if(count == Tools.totalMine){
            JOptionPane.showMessageDialog(null, Tools.win);
            startFrame.getMineState().getFace().setIcon(Tools.iiFace[4]);
            startFrame.getTimer().stop();
            for (int i = 0; i < Tools.rows ; i++) {
                for (int j = 0; j < Tools.cols; j++) {
                    startFrame.getMineField().getMineLabels()[i][j].removeMouseListener(startFrame.getMineField().getMineLabelListener());
                }
            }
        }

    }

}
